package com.asm.handler;

import com.asm.message.GroupJoinRequestMessage;

import com.asm.message.GroupJoinResponseMessage;
import com.asm.server.session.Group;
import com.asm.server.session.GroupSessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.List;
/**
 * 加入群聊成员
 * */
@ChannelHandler.Sharable
public class GroupJoinRequestMessageHandler extends SimpleChannelInboundHandler<GroupJoinRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupJoinRequestMessage msg) throws Exception {
       //获取加入的群名称
        String groupName = msg.getGroupName();
        // 获取拉你入群的是谁
        String username = msg.getUsername();

        //添加到群组
        Group group = GroupSessionFactory.getGroupSession().joinMember(groupName, username);

        if(group!=null){

          ctx.writeAndFlush(new GroupJoinResponseMessage(true,username+"加入群聊成功"));
      }else {
            ctx.writeAndFlush(new GroupJoinResponseMessage(false,username+"加入群聊失败或不存在"));
        }
    }
}
